from time import sleep
from tkinter import ttk, scrolledtext, Frame
from tkinter import *
from ModeType.Base import BaseOutput
from pynput import mouse, keyboard

kayCon = keyboard.Controller()
mouCon = mouse.Controller()

def pressKey(k):
    kayCon.type(k)

def mouseMove(x, y):
    mouCon.position = (x, y)

def mouseClick(count = 1):
    mouCon.click(count=count)

# 键鼠自动化脚本

class KeyboardMouseScriptResult(BaseOutput):
    def createConfig(self, root):
        box = ttk.LabelFrame(root, text='键鼠自动化脚本')
        box.pack(padx=10, pady=10)

        innerBox = Frame(box)
        innerBox.grid(row=0, column=0, columnspan=2)

        def pressKeyCode():
            self.code.insert(INSERT, "pressKey('a')\n")
        ttk.Button(innerBox, text="键盘输入", command=pressKeyCode).grid(row=0, column=0)

        def mouseMoveCode():
            self.code.insert(INSERT, "mouseMove(1, 2)\n")
        ttk.Button(innerBox, text="鼠标移动", command=mouseMoveCode).grid(row=0, column=1)

        def mouseClickCode():
            self.code.insert(INSERT, "mouseClick(1)\n")
        ttk.Button(innerBox, text="鼠标点击", command=mouseClickCode).grid(row=0, column=2)

        def sleepCode():
            self.code.insert(INSERT, "sleep(1)\n")
        ttk.Button(innerBox, text="延迟", command=sleepCode).grid(row=0, column=3)
        
        ttk.Label(box, text='内容').grid(row=1, column=0)
        self.code = scrolledtext.ScrolledText(box, height=30)
        self.code.grid(row=1, column=1)
        self.code.insert(INSERT, 'print(123456)')

        return box

    def run(self):
        code = self.code.get(1.0, END)
        exec(code)
        
